#include "Sprite.h"


Sprite::Sprite(Texture* image, int totalFrame, int spritePerRow, float timePerFrame)
{
	_image = image;

	_index = 0;
	_spritePerRow = spritePerRow;
	_totalFrame = totalFrame;

	_width = _image->GetWidth() / spritePerRow;
	_height = _image->GetHeight() / (_totalFrame / spritePerRow);

	_timePerFrame = timePerFrame;
	_timeWait2Frame = 0;
}


Sprite::~Sprite()
{

}

void Sprite::Render(LPD3DXSPRITE spriteHandler, float x, float y)
{
	D3DXVECTOR3 positionRender(x, y, 0);
	RECT srect = GetDrawRect();
	D3DXVECTOR3 center(_width / 2, _height / 2, 0);

	spriteHandler->Draw(_image->GetImage(), &srect, &center, &positionRender, D3DCOLOR_XRGB(255, 255, 255));
}

void Sprite::Render(LPD3DXSPRITE spriteHandler, float x, float y, bool flipY, Viewport* viewport)
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	matrix._22 = -1;
	matrix._41 = -viewport->GetPosX();
	matrix._42 = viewport->GetPosY();;

	D3DXVECTOR3 position(x, y, 0);
	D3DXVECTOR4 ps;
	D3DXVec3Transform(&ps, &position, &matrix);

	if (flipY == true)
	{
		D3DXVECTOR2 center_scale(ps.x, ps.y);
		D3DXVECTOR2 scale(-1, 1);

		D3DXMatrixTransformation2D(&matrix, &center_scale, 0, &scale, NULL, NULL, NULL);

		spriteHandler->SetTransform(&matrix);
	}

	D3DXVECTOR3 positionRender(ps.x, ps.y, 0);
	RECT srect = GetDrawRect();
	D3DXVECTOR3 center(_width / 2, _height / 2, 0);

	spriteHandler->Draw(_image->GetImage(), &srect, &center, &positionRender, D3DCOLOR_XRGB(255, 255, 255));

	if (flipY == true)
	{
		D3DXMatrixTransformation2D(&matrix, NULL, 0.0f, NULL, NULL, 0.0f, NULL);
		spriteHandler->SetTransform(&matrix);
	}
}

void Sprite::Render(LPD3DXSPRITE spriteHandler, float x, float y, float zoomX, float zoomY, float angle, Viewport* viewport)
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	matrix._22 = -1;
	matrix._41 = -viewport->GetPosX();
	matrix._42 = viewport->GetPosY();

	D3DXVECTOR3 position(x, y, 0);
	D3DXVECTOR4 ps;
	D3DXVec3Transform(&ps, &position, &matrix);

	D3DXVECTOR2 center_scale(ps.x, ps.y);
	D3DXVECTOR2 scale(zoomX, zoomY);
	D3DXMatrixTransformation2D(&matrix, &center_scale, 0, &scale, &center_scale, angle, NULL);
	spriteHandler->SetTransform(&matrix);

	D3DXVECTOR3 positionRender(ps.x, ps.y, 0);
	RECT srect = GetDrawRect();
	D3DXVECTOR3 center(_width / 2, _height / 2, 0);

	spriteHandler->Draw(_image->GetImage(), &srect, &center, &positionRender, D3DCOLOR_XRGB(255, 255, 255));

	D3DXMatrixTransformation2D(&matrix, NULL, 0.0f, NULL, NULL, 0.0f, NULL);
	spriteHandler->SetTransform(&matrix);
}

void Sprite::Render(LPD3DXSPRITE spriteHandler, float x, float y, RECT rect, Viewport* viewport)
{
	

	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	matrix._22 = -1;
	matrix._41 = -viewport->GetPosX();
	matrix._42 = viewport->GetPosY();;

	D3DXVECTOR3 position(x, y, 0);
	D3DXVECTOR4 ps;
	D3DXVec3Transform(&ps, &position, &matrix);

	D3DXVECTOR3 positionRender(ps.x, ps.y, 0);
	D3DXVECTOR3 center(_width / 2, _height / 2, 0);

	spriteHandler->Draw(_image->GetImage(), &rect, &center, &positionRender, D3DCOLOR_XRGB(255, 255, 255));
}

void Sprite::Render(LPD3DXSPRITE spriteHandler, float x, float y, RECT rect)
{
	D3DXVECTOR3 position(x, y, 0);
	D3DXVECTOR3 center(_width / 2, _height / 2, 0);

	spriteHandler->Draw(_image->GetImage(), &rect, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
}

RECT Sprite::GetDrawRect()
{
	RECT srect;

	srect.left = (_index % _spritePerRow) * _width;
	srect.top = (_index / _spritePerRow) * _height;
	srect.right = srect.left + _width;
	srect.bottom = srect.top + _height;

	return srect;
}

void Sprite::Next(float time)
{
	if (_timeWait2Frame > _timePerFrame)
	{
		_index = (_index + _totalFrame - 1) % _totalFrame;

		_timeWait2Frame = 0;
	}
	else
		_timeWait2Frame += time;

}

void Sprite::Next(int min, int max, float time)
{
	if (_timeWait2Frame > _timePerFrame)
	{
		if (_index < --min || _index > --max)
			_index = min;

		_index++;

		_timeWait2Frame = 0;
	}
	else
		_timeWait2Frame += time;
}


void Sprite::Next(int index)
{
	_index = index;
}